Skip to content

Give each network namespace its own socktab - #156

Open
Stuartwastaken wants to merge 5 commits into
PlatformLab:mainfrom
Stuartwastaken:netns-socktab
Open

Stuartwastaken wants to merge 5 commits into
PlatformLab:mainfrom
Stuartwastaken:netns-socktab

Conversation

@Stuartwastaken

@Stuartwastaken Stuartwastaken commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

This is the first step of the namespace work. Today the sockets of all network namespaces share homa->socktab, and each lookup compares hsk->hnet. This moves the socktab into struct homa_net, so each namespace has its own table and its own port space.

homa_timer still has to visit every socket. It walks the kernel's net_namespace_list with for_each_net_rcu and scans each namespace's socktab. The scan calls schedule(), so it can't stay inside the RCU read lock: it takes a reference on the namespace with maybe_get_net, drops the RCU lock for the scan, and takes it again before put_net. The scans in homa_devel.c don't block, so they use for_each_net_rcu alone. Homa keeps no list of namespaces of its own, and there is no new lock.

This also fixes homa_abort_rpcs, which scanned the shared table without checking the namespace, so an ICMP unreachable received in one namespace aborted RPCs to the same address in all of them. It now takes the hnet from the error handler and scans only that table; homa_abort_rpcs__ignore_other_hnets fails without the change. Ashish's #143 contains the same fix for homa_abort_rpcs (an hnet argument plus a check in the loop), so you can take either one; whichever goes in second needs a small rebase.

Each namespace now allocates its own socktab (1024 buckets, a little over 8 KB) while Homa is loaded. homa_destroy no longer deletes sockets, so each unit-test fixture's teardown calls mock_hnets_destroy first.

Besides the unit tests, I ran this on 6.17.8 with the same port bound in four namespaces at once, RPCs between two namespaces over a veth pair, and 300 namespace create/delete cycles under load. That kernel has neither KASAN nor lockdep, so those have not been run.

Before this change all namespaces shared homa->socktab, with the
namespace as part of the hash key. Now each homa_net has its own
socktab, and homa->nets links the homa_nets together so that
homa_timer can scan the sockets of all namespaces.

This fixes a bug in homa_abort_rpcs: an ICMP error received in one
namespace aborted RPCs to the same address in every namespace.
@Stuartwastaken
Stuartwastaken marked this pull request as ready for review November 23, 2026 17:23

@johnousterhout johnousterhout left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good start. I made a quick scan and added a few comments. Let's discuss the ones labeled "Let's discuss".

Comment thread homa_impl.h Outdated
* holding either @nets_mutex or the RCU read lock.
*/
struct homa_socktab *socktab;
struct list_head nets;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make this an RCU list so that nets_mutex doesn't need to be held while traversing it? Or, if this is already an RCU list, document that explicitly. If it can be scanned with only an RCU read lock, why would anyone want to use nets_mutex? Let's discuss.

Comment thread homa_impl.h Outdated
struct list_head nets;

/** @nets_mutex: Synchronizes access to @nets. */
struct mutex nets_mutex;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The convention in Homa is to use spinlocks for everything, unless there is a particular reason why a sleep lock is needed.

Comment thread homa_impl.h Outdated
* homa->nets_mutex, all locks should be considered "leaf" locks: don't
* acquire other locks while holding them.
* 2. The lock order is:
* * homa->nets_mutex

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a new layer in the locking hierarchy is a major change; it's not obvious to me that this is necessary. Let's discuss.

Comment thread homa_plumbing.c Outdated
void homa_net_pre_exit(struct net *net)
{
homa_net_unlink(homa_net(net));
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a pass-through function; why not just put homa_net_unlink into the struct pernet_operations? Let's discuss.

Comment thread homa_sock.c Outdated

return homa_socktab_next(scan);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this function needed? Why not just let callers do the 2-level scan themselves (iterate over the list of hnets, then invoke homa_socktab_start_scan on each)? Let's discuss.

Comment thread homa_sock.h Outdated
* struct homa_socktab - A hash table that maps from port numbers (either
* client or server) to homa_sock objects.
* client or server) to homa_sock objects. There is one of these for each
* network namespace.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this additional comment is needed. Is there anything in this module that needs to care about the existence of network namespaces? Ideally all of the network namespace can be built on top of this module, without this module needing to know how it is being used.

Comment thread homa_timer.c Outdated
* RCU to keep network namespaces from being deleted during the scan,
* because the scan may block.
*/
mutex_lock(&homa->nets_mutex);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where/how does the scan block? Given that RCU is already used below when scanning an individual socket, why not just use it for namespace scan also? Let's discuss.

Comment thread homa_utils.c Outdated
list_for_each_entry_safe(hnet, next, &homa->nets, links) {
homa_net_unlink(hnet);
homa_socktab_destroy(hnet->socktab);
kfree(hnet->socktab);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having a bunch of code here that only matters for unit tests is awkward (and can't be upstreamed). Can you find a way to get that code out of here and put it someplace in the unit testing framework instead?

@breakertt

Copy link
Copy Markdown
Contributor

Thanks for starting this. I have been testing Homa between namespaces
(veth pairs, and pods routed through the host) and hit a few problems
as well. Have you seen any of them?

  1. Over a veth pair, messages longer than one packet never complete.
    veth delivers the GSO packet unsegmented, and the receive side
    assumes one segment per skb. Did your veth tests use multi-packet
    messages?
    Same for virtio without gso force software
  2. When the host forwards Homa packets to a namespace (ip_forward=1),
    homa_gro_receive merges them as local packets; the merged skb can
    then hit the BUG in skb_segment on the way out.
  3. The net.homa.* sysctls are registered only in init_net, so a
    namespace cannot see or set them.

I have local fixes for 1 and 2, but they are not cleaned up for
upstream yet. I am happy to do that and send PRs.

@Stuartwastaken

Copy link
Copy Markdown
Contributor Author

This is a good start. I made a quick scan and added a few comments. Let's discuss the ones labeled "Let's discuss".

Happy to discuss, @johnousterhout. Our call this week was cancelled by Thiya but we can pick this up next week. Happy Thanksgiving! Perusing through here I can understand my own naive mistake of doing a mutex lock on the whole namespace table, and not taking care of layering correctly by involving the socket code. And actually while doing this I learned that linux keeps track of the namespaces for us, so that makes this easier. Pushing some fixes now to use the linux namespace, keeping the socktab code separate and namespace-unaware, and making sure the unit test code stays out of the main code.

To answer your question about where the scan blocks: it's only the schedule() every 10 RPCs. My plan is for the timer to take a reference on each namespace (maybe_get_net), drop the RCU lock while it scans that namespace's sockets, then retake RCU and release the reference before moving on.

Replaces homa->nets and homa->nets_mutex. homa_timer now walks the
kernel's list of namespaces under RCU and holds a reference on each
namespace while it scans that namespace's sockets. The socktab code
no longer knows about namespaces, and the unit-test cleanup of
homa_nets has moved from homa_destroy to mock.c.
@Stuartwastaken

Copy link
Copy Markdown
Contributor Author

Thanks Tianyi. I can reproduce 1; I haven't tested 2; 3 I knew about.

  1. Yes, they were multi-packet: 100 KB, 500 KB and 1 MB echo RPCs between two namespaces over a veth pair, both directions, IPv4, and all of them completed with the data checked. But I had left max_gso_size at its default of 1000, and with that homa_message_out_init computes max_gso_segs = 1, so every skb carried one segment and no GSO packet ever crossed the veth. When I set max_gso_size to 20000 (6.17.8), messages up to 1400 bytes still complete and a 10000-byte message hangs. From reading the code I agree with your explanation: the skb is marked SKB_GSO_TCPV4/6, veth has TSO so it passes it through whole, and homa_add_packet takes homa_data_len(skb) as the length of one segment. What max_gso_size were you running?
  2. I haven't seen it. My two namespaces were joined directly by the veth pair, so nothing was forwarded through the host. It makes sense from the code: homa_gro_receive runs before routing and adds any Homa packet to the held skb's frag_list, without knowing whether the packet is for a local socket.
  3. Yes. All four register_net_sysctl calls (homa_plumbing.c, homa_grant.c, homa_peer.c, homa_qdisc.c) pass &init_net, and the values are in the global struct homa. /proc/net/homa_metrics is created only in init_net too. I left this out of the PR on purpose; whether those settings should become per-namespace is one of the things I want to ask John.

None of the three is touched by this PR (it only moves the socket table), so fixes for 1 and 2 shouldn't conflict with it. They may be worth checking against #143 though, since it changes both paths (SKB_GSO_UDP_L4 on send, a UDP GRO callback on receive).

@Stuartwastaken

Stuartwastaken commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor Author

Also @johnousterhout I have another question for this implementation. Right now each namespace has a 1024-bucket table (8kb of bucket heads, kmalloc brings this up to 16kb), and on every timer tick (1ms) the kernel will scan every bucket in the namespace table, even if its empty. So with 500 network namespaces, that would be about 4MB of mostly empty buckets every millisecond. An alternative approach I can do in this same PR is to skip a namespace whose table is empty (by adding a socket count in homa_socktab). That'd be more lightweight. Testing this on my VM as well.

Scanning the socktab of a namespace takes about 2.8 usec per tick even
if it has no sockets, which adds up when there are many namespaces.
@Stuartwastaken

Copy link
Copy Markdown
Contributor Author

I went ahead and pushed this as a separate commit so you can see it: num_socks in homa_socktab, and homa_timer skips a namespace when it is zero. On my VM an empty namespace cost 2.8 usec per tick before and 0.08 usec after; with 1000 namespaces a timer pass went from 2.85 ms to 83 usec. Easy to drop if you'd rather handle it differently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants